﻿using System;
using System.Collections.Generic;
using System.Text;
using lin;

public delegate void Handler(UserToken token,byte[] bytes);

public class HanderCenter
{
    Dictionary<Protocol, Handler> handlers = new Dictionary<Protocol, Handler>();

    public HanderCenter()
    {
        LoginHandler login = new LoginHandler(handlers);
        GameHandler game = new GameHandler(handlers);
    }

    public void OnReceive(UserToken token, Protocol command, byte[] content)
    {
        handlers[command](token, content);
    }

    public void Disconnect(UserToken token)
    {
        if (token.user != null)
        {
            Console.WriteLine("玩家{0}退出了游戏", token.user.username);
            UserManager.Instance.DelUser(token);

            //如果玩家在游戏中
            if(token.user!=null && token.user.roomId!=0)
            {
                Room rm = RoomManager.Instance.GetRoom(token.user.roomId);
                rm.Offline(token);
            }
        }
    }

    public void Connect(UserToken token)
    {
        UserManager.Instance.AddUser(token);
    }


}
